﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;

namespace LPV_POS.mh_pos
{
    public partial class mh_banhang4_DVT : DevExpress.XtraEditors.XtraForm
    {
        DataRow drDVT = null;
        public mh_banhang4_DVT(string _MA_HH, string _Ten_HH)
        {
            InitializeComponent();
            string sSQL = "";
            thuvien othuvien = new thuvien();

            // -== -== -== -== -== -== -== -== -== -== -== -== -== -== -== -== -== -== -== -== -== -== -== -== -== -== -== -==
            // -== -== -== -== -== -== -== -== -== -== -== -== -== -== -== -== -== -== -== -== -== -== -== -== -== -== -== -==
            sSQL = "SELECT a.MAHH, a.DVT_FIRST, a.SL_FIRST, a.DONGIA, a.DVT_LAST, a.SL_LAST, b.OBJNAME, " +
                    "       STT = (ROW_NUMBER()OVER(ORDER BY a.MAHH))%2 " +
                    "FROM DM_DOIDVT a " +
                    "       LEFT JOIN DM_DVT b ON a.DVT_LAST = b.OBJID " +
                    "WHERE a.MAHH LIKE '" + _MA_HH + "' ";
                    

            DataTable tbMain = othuvien.SP_SelectDataTable("SP_EXECUTESQL", new string[] { "@sSQL" }, new object[] { sSQL });
            gridControl1.DataSource = tbMain;
            lbel_tencongty.Text = _Ten_HH.ToUpper();

            sSQL = "SELECT HINH FROM DM_HH WHERE OBJID LIKE '" + _MA_HH + "'";
            DataTable tbHinh_HH = othuvien.SP_SelectDataTable("SP_EXECUTESQL", new string[] { "@sSQL" }, new object[] { sSQL });
            
            if (tbHinh_HH != null)
                if (tbHinh_HH.Rows.Count > 0)
                    this.pictureBox1.Visible = this.SetHinhAnh(pictureBox1, tbHinh_HH.Rows[0]["HINH"]);
                    
        }

        
        private void mh_banhang3_ChuyenBan_Load(object sender, EventArgs e)
        {

            
        }

        private void btnThoat_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.Cancel;
            this.Dispose();
        }

        private void btnThucHien_Click(object sender, EventArgs e)
        {
            drDVT = gridView1.GetDataRow(gridView1.FocusedRowHandle);
            
            this.DialogResult = DialogResult.OK;
            this.Close();

        }
        public DataRow ShowForm()
        {
            this.ShowDialog();

            return drDVT;
        }
        Boolean SetHinhAnh(System.Windows.Forms.Control ctrl, Object _HinhAnh)
        {
            //ctr:  ?.....Yc
            //img: Xi.....Yi
            //Xi = (Xi * Yc) / Yi
            try
            {
                if (_HinhAnh + "" != "")
                {
                    System.Drawing.Image img = new cpublic().ConvertByteArrayToImage(_HinhAnh);
                    float Xi = (float)img.Width;
                    float Yi = (float)img.Height ;
                                        
                    float Yc = (float)ctrl.Size.Height;
                    int Xc = (int)((Xi * Yc) /Yi);

                    ctrl.Size = new Size(Xc, (int)Yc);

                    ctrl.BackgroundImage = new cpublic().ConvertByteArrayToImage(_HinhAnh);
                    return true;
                }
            }
            catch (Exception ex)
            {
                string s = ex.Message;
            }
            return false;
        }
    }
}